
package com.gsit.job.services;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.gsit.job.domain.SearchCriteria;
import com.gsit.job.domain.SearchStatus;
import com.gsit.job.domain.SearchType;

/**
 * A SearchCriteriaService
 */
@Repository("searchCriteriaService")
@Qualifier("searchCriteriaService")
public class SearchCriteriaService {
	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public List<SearchCriteria> findAll() {
		return entityManager.createNamedQuery("SearchCriteria.findAll",SearchCriteria.class).getResultList();
	}
	
	@Transactional
	public SearchCriteria findSearchCriteriaByStatus(SearchStatus searchStatus) {
		 Query query = entityManager.createQuery("select searchCriteria from SearchCriteria searchCriteria where searchCriteria.searchStatus=:searchStatus order by searchCriteria.priority",SearchCriteria.class);
		 query.setParameter("searchStatus", searchStatus);
		 List<SearchCriteria> criterias = query.getResultList();
		 SearchCriteria criteria = null;
		 if(criterias.size() > 0){
			 criteria = criterias.get(0);
		 }
		 return criteria;
	}
	
	@Transactional
	public SearchCriteria findSearchCriteriaByStatusAndType(SearchStatus searchStatus,SearchType searchType) {
		 Query query = entityManager.createQuery("select searchCriteria from SearchCriteria searchCriteria where searchCriteria.searchStatus=:searchStatus and searchCriteria.searchType=:searchType order by searchCriteria.priority",SearchCriteria.class);
		 query.setParameter("searchStatus", searchStatus);
		 query.setParameter("searchType", searchType);
		 List<SearchCriteria> criterias = query.getResultList();
		 SearchCriteria criteria = null;
		 if(criterias.size() > 0){
			 criteria = criterias.get(0);
		 }
		 return criteria;
	}

	@Transactional
	public <T> T find(Class<T> entityClass, Long id) {
		return entityManager.find(entityClass, id);
	}

	@Transactional
	public SearchCriteria saveOrUpdate(SearchCriteria entity) {
		return entityManager.merge(entity);
	}

	@Transactional
	public void delete(SearchCriteria entity) {
		entityManager.remove(entityManager.merge(entity));
	}
	

}
